package com.admin;



import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;







public class UserService extends CompanyService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8855712941642484187L;
	private  String table="\"User\"";
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  User selectLoginUser(Map pharm) {
		User user=new User(); 
		String where="1=1";
		Iterator<String> iter = pharm.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) pharm.get(key);
			   where+=" and \""+key+"\"='"+value+"'";
		}
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		 user=selectuerbysql(map);
		return user;
	}
	
	@SuppressWarnings({ "rawtypes" })
	protected   User selectuerbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		User user=new User(); 
		try {
		UserMapper usermp=sqlSession.getMapper(UserMapper.class);
		user=usermp.selectuerbysql(map);
		
		} finally {
			sqlSession.close();
		}
		return user;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected  List<User> getalluser(int Comid,String tj) {
		String where="";
		if(Comid==0) {
			if(tj.equals("")) {
				
				where=" 1=1 ";
			}else {
				
				where=tj;
			}
			
		}else {
if(tj.equals("")) {
				
	where=" d.\"Comid\"="+Comid+" ";	
			}else {
				where=tj+" and d.\"Comid\"="+Comid;		
				
			}
		}
		
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		List<User> user=selectuerlistbysql(map);
		return user;
	
	}

	@SuppressWarnings("rawtypes")
	protected  List<User> selectuerlistbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		List<User> user;
		try {
			UserMapper usermp=sqlSession.getMapper(UserMapper.class);
			user=usermp.selectuerlistbysql(map);
			
		}finally {
			sqlSession.close();
			
	 }
		return user;
	}
	
	//根据用户账号查询密码
	protected String selectpasswordByUsercode(String usercode){
		String result;
		SqlSession sqlSession = getSqlSession();
		try{
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectpasswordByUsercode(usercode);
		}finally{
			sqlSession.close();
		}
		return result;
	}
	
	//修改密码
	@SuppressWarnings("rawtypes")
	protected int updatepassword(Map map){
		int result=0;
		SqlSession sqlSession =getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updatepassword(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//搜索用户表单list
	@SuppressWarnings("rawtypes")
	protected List<User> selectusertable(Map map){
		List<User> list;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			list = mapper.selectusertable(map);
		} finally {
			sqlSession.close();
		}
		return list;
	}
	
	//新增搜索usercode是否重复
	@SuppressWarnings("rawtypes")
	protected List<User> selectusercode(Map map){
		List<User> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectusercode(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//修改搜索username是否重复
	@SuppressWarnings("rawtypes")
	protected List<User> selectusername(Map map){
		List<User> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectusername(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//通过选中的角色id查询对应的公司id
	protected List<Company> selectComidByRoleid(String Roleid){
			List<Company> result;
			SqlSession sqlSession = getSqlSession();
			try {
				CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
				result = mapper.selectComidByRoleid(Roleid);
			} finally {
				sqlSession.close();
			}
			return result;
		}
	
	
	//取出新增的用户id
	@SuppressWarnings({ "rawtypes" })
	protected String selectUseridByUsercode(Map map){
		String result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectUseridByUsercode(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	
	@SuppressWarnings("rawtypes")
	protected String selectComidByDeptid(Map map){
		String result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectComidByDeptid(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	
	
	
	//启用新增用户默认角色
	@SuppressWarnings("rawtypes")
	protected int updateuserrolechecked(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updateuserrolechecked(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//启用新增用户默认公司
	@SuppressWarnings("rawtypes")
	protected int updateusercompanychecked(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updateusercompanychecked(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//编辑用户账号
	@SuppressWarnings("rawtypes")
	protected int updateusercode(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updateusercode(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//编辑用户名称
	@SuppressWarnings("rawtypes")
	protected int updateusername(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updateusername(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//用户状态停启用
	@SuppressWarnings("rawtypes")
	protected int updateForbidden(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.updateForbidden(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//用户删除
	@SuppressWarnings("rawtypes")
	protected int deleteUsers(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.deleteUsers(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//查询用户已开通公司
	@SuppressWarnings("rawtypes")
	protected List<Company> selectOwnCompany(Map map){
		List<Company> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectOwnCompany(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//查询用户已开通角色
	@SuppressWarnings("rawtypes")
	protected List<Role> selectOwnRole(Map map){
		List<Role> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectOwnRole(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//删除之前添加的公司及角色
	@SuppressWarnings("rawtypes")
	protected int deleteUserCompany(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.deleteUserCompany(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//重置用户密码
	@SuppressWarnings("rawtypes")
	protected int resetUserPassword(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.resetUserPassword(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//查询部门是否存在
	@SuppressWarnings("rawtypes")
	protected List<User> selectDeptid(Map map){
		List<User> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectDeptid(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//查询角色是否存在
	@SuppressWarnings("rawtypes")
	protected List<Role> selectRoleid(Map map){
		List<Role> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectRoleid(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//通过用户ID查询对应公司id
	@SuppressWarnings("rawtypes")
	protected List<Company> selectComidByUserid(Map map){
		List<Company> result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectComidByUserid(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
}
